﻿using CaterBll;
using CaterModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CaterUI
{
    public partial class FormDishTypeInfo : Form
    {
        public FormDishTypeInfo()
        {
            InitializeComponent();
        }
        private void FormDishTypeInfo_Load(object sender, EventArgs e)
        {
            GetList();
        }
        private DishTypeInfoBll dtiBll = new DishTypeInfoBll();
        private int rowIndex = -1;
        DialogResult result = DialogResult.Cancel;
        private void GetList()
        {
            dgvDishTypeInfo.AutoGenerateColumns = false;
            dgvDishTypeInfo.DataSource = dtiBll.GetList();
          
            //设置选中行
            if(rowIndex>0)
            {
                dgvDishTypeInfo.Rows[rowIndex].Selected = true;
            }
        }

        private void dgvDishTypeInfo_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            btnSave.Text = "修改";
            var row =dgvDishTypeInfo.Rows[e.RowIndex];

            txtDid.Text = row.Cells[0].Value.ToString();
            txtDtitle.Text = row.Cells[1].Value.ToString();

            //记录被点击的行的索引，用于刷新后再次选中
            rowIndex = e.RowIndex;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            DishTypeInfo dti = new DishTypeInfo();
            
            dti.DTitle = txtDtitle.Text.ToString();
            if(txtDid.Text=="添加时无编号")
            {
                //添加
               if( dtiBll.Insert(dti)>0)
                {
                    MessageBox.Show("添加成功！");
                    GetList();
                }
               else
                {
                    MessageBox.Show("添加失败！");
                }
            }
            else
            {
                dti.DId =Convert.ToInt32(txtDid.Text);
                //修改
                if (dtiBll.Update(dti) > 0)
                {
                    MessageBox.Show("修改成功！");
                    GetList();
                }
                else
                {
                    MessageBox.Show("修改失败！");
                }
            }
            CleanData();

            result = DialogResult.OK;
        }

        private void CleanData()
        {
            txtDid.Text = "添加时无编号";
            txtDtitle.Text = "";
            btnSave.Text = "添加";
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            CleanData();
        }

        private void FormDishTypeInfo_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult = result;
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult result = MessageBox.Show("确定要删除吗？", "删除提示", MessageBoxButtons.OKCancel);
                if(result==DialogResult.OK)
                {
                    int id =Convert.ToInt32(dgvDishTypeInfo.SelectedRows[0].Cells[0].Value);
                    if(dtiBll.Delete(id)>0)
                    {
                        MessageBox.Show("删除成功！");
                    }
                    else
                    {
                        MessageBox.Show("删除失败！");
                    }
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "错误提示");
            }
        }
    }
}
